package com.harjeethj.wheresmymaggi;

import web.product;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;


public class MainActivity extends Activity {

	String[] productsarray;
	String products;
	AutoCompleteTextView actv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
         
       
       Button btn_continue = (Button)findViewById(R.id.btn_continue);
       btn_continue.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {

				AutoCompleteTextView av1 = (AutoCompleteTextView) findViewById(R.id.txt_product_name);

				web.get_product_details get_details = new web.get_product_details();
				product p1 = get_details.getProduct_details(av1.getText().toString());

				if (p1 == null) {
					AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
							MainActivity.this);

					dlgAlert.setMessage(" Searched Product is not available,Try for different item ");
					dlgAlert.setTitle("Sorry");
					dlgAlert.setPositiveButton("OK", null);
					dlgAlert.setCancelable(true);
					dlgAlert.create().show();
				} 
				else {
					Intent intent = new Intent(MainActivity.this,
							Fetch_product_details.class);
					//intent.putExtra("p_Name", av1.getText().toString());
					intent.putExtra("Object",p1);
					
					startActivity(intent);
				}

			}
		});
	 	
      		//Getting the instance of AutoCompleteTextView  
		      actv= (AutoCompleteTextView)findViewById(R.id.txt_product_name);  
		      actv.setThreshold(1);//will start working from first character  
		      actv.setTextColor(Color.GRAY);  
       
       actv.addTextChangedListener(new TextWatcher(){
    	   @Override
           public void onTextChanged(final CharSequence s, int start, int before, int count) {
    		
    		   
    		   
    		   
    		   /**
      			 * 
      			 * Thread method for population dropdown for search product 
      			 * 
      			 */
      			  		
      			Thread load_values = new Thread() {
      				public void run() {
      					
      					try {
      						
      						web.ListproductsUserInput w1=new web.ListproductsUserInput();
     						 products=w1.getallproducts(s.toString());
     		 	 	       	 if(products.length()>2)
     		 	 	       	 {
     		 	 	       		 products=products.substring(0,products.length()-1);
     		 	 	       		 productsarray = products.split(",");
     		 	 	       		 	runOnUiThread(new Runnable(){

													@Override
													public void run() {
													ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.select_dialog_item,productsarray);
													actv.setAdapter(adapter);  //setting the adapter data into the AutoCompleteTextView
							      			       	actv.showDropDown();
													}
					      							
		     		 	 	       		 	});
		      						
		     		 	 	       	 	}	 	 	    
		      						} catch (Exception e) {
		      						System.out.print(e.toString());
		      					
		      					}
		      				}
		      			};
		      			
		      			// start thread
		      			Thread.currentThread().interrupt();
		      			load_values.start();	
		   		 
		      			
		      			/**
		      			 * 
		      			 * Thread method for population dropdown for search product 
		      			 * 
		      			 */	
   			
    		  
    	
    	   		}
    	   
    	   
    	   

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			
		
			
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			
		}
       
    	   
       });
       
       
       
        
        
    }
    


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
